	//게시판 타입이 변경되었을경우 db 생성버튼 추가
	function showBtn() {
		var brdType = $("#menu_type").val();
		if (("1" == brdType || "2" == brdType || "3" == brdType || "4" == brdType)
				&& "" != $("#menu_id").val()) {
			$("#btnCreateDb").show();
		} else {
			$("#btnCreateDb").hide();
		}
	}

	var seqChkDb = true;
	//db 생성버튼
	function createDb() {
		var brdType = $("#menu_type").val();
		// 	alert($("#menu_id").val());
		// 	return false;
		if (("1" == brdType || "2" == brdType || "3" == brdType || "4" == brdType)
				&& "" != $("#menu_id").val()) {
			if (seqChkDb) {
				seqChkDb = false;
				var opt = {
					type : "get",
					url : "./createDb.do",
					dataType : "json",
					data : {
						"menu_seq" : $("#menu_id").val(),
						"brd_type" : brdType
					},
					success : function(json) {
						console.log(json);
						if ('Y' == json.code) {
							alert("DB 생성 완료");
						} else {
							alert("DB 생성 에러");
						}
					},
					error : function(error) {
						seqChk = true;
						alert('접속지연이 발생했습니다. 몇분후에 다시 확인해주세요\n' + error);
					}
				};
				$.ajax(opt);
			} else {
				alert("진행중입니다.");
			}
		}
	}

	var seqChk = true;
	//메뉴 추가
	function addMenu() {
		if (seqChk) {
			seqChk = false;
			var opt = {
				type : "get",
				url : "./getMenuSeq.do",
				dataType : "json",
				success : function(json) {
					var menuSeq = json.menuSeq;
					var html = "<li class=\"dd-item menuList"+menuSeq+"\" data-id=\""+menuSeq+"\" menu_nm=\"Menu Name\" menu_depth=\"0\" menu_url=\"\" menu_type=\"\" view_stat=\"Y\" user_write=\"\" use_yn=\"Y\">"
							+"<div class=\"dd-handle ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-arrow-2-n-s\"></span></div>"
							+"<div class=\"dd-content ui-state-default\">"
							+"<span class=\"menu_nm\">Menu Name</span> "
							+"	<div class=\"tools\">"
							+"		<a href=\"javascript:editMenu("
							+ menuSeq
							+ ");\" class=\"ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-wrench\"></span></a>"
							+"		<a href=\"javascript:deleteMenu("
							+ menuSeq
							+ ");\" class=\"ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-circle-minus\"></span></a>"
							+"	</div>"
							+"</div>"
//							+ "    <div class=\"dd-handle ui-state-default\"><span class=\"menu_nm\">신규 추가</span> "
//							+ "<a href=\"#\" onclick=\"editMenu("
//							+ menuSeq
//							+ ");\" class=\"icon icon-edit\"></a> "
//							+ "<a href=\"#\" onclick=\"deleteMenu("
//							+ menuSeq
//							+ ");\" class=\"icon icon-trash\"></a>"
//							+"<div class=\"tools\">"
//							+"<a href=\"javascript:editMenu("
//							+ menuSeq
//							+ ");\" class=\"ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-video\"></span></a>"
//							+"<a href=\"javascript:deleteMenu("
//							+ menuSeq
//							+ ");\" class=\"ui-state-default ui-corner-all\"><span class=\"ui-icon ui-icon-wrench\"></span></a>"
//							+"<button type=\"button\" onclick=\"alert(22);\" class=\"btn btn-default btn-xs \">"
//							+"	<span class=\"glyphicon glyphicon-star\"></span>"
//							+"</button>"
							+"</div>"
//							+ "</div></li>";
					$("#menuOl").append(html);
					$('#nestable3').nestable();
					seqChk = true;
				},
				error : function(error) {
					seqChk = true;
					console.log(error);
					alert('접속지연이 발생했습니다. 몇분후에 다시 확인해주세요\n' + error);
				}
			};

			$.ajax(opt);
		} else {
			alert("잠시만 기다려 주세요. 처리중입니다.");
		}
	}

	//메뉴 저장
	function saveMenu() {

		$(".menuList" + $("#menu_id").val()).find(".menu_nm:eq(0)").html(
				$("#menu_nm").val());

		$(".menuList" + $("#menu_id").val()).attr("menu_nm",
				$("#menu_nm").val());
		$(".menuList" + $("#menu_id").val()).attr("menu_type",
				$("#menu_type").val());
		if ($("#user_write").is(":checked")) {
			$(".menuList" + $("#menu_id").val()).attr("user_write", 'Y');
		} else {
			$(".menuList" + $("#menu_id").val()).attr("user_write", '');
		}
		if ($("#use_yn").is(":checked")) {
			$(".menuList" + $("#menu_id").val()).attr("use_yn", 'Y');
		} else {
			$(".menuList" + $("#menu_id").val()).attr("use_yn", '');
		}

	}

	var staticDepth = 0;
	//depth 알아오기
	function chkDepth() {
		$(".dd-item").each(function() {
			staticDepth = 0;
			chkDepthLoop(this);
			$(this).attr("menu_depth", staticDepth);
		});
	}

	//depth 루프
	function chkDepthLoop(doc) {
		// 	console.log(doc);
		var doc1 = $(doc);
		var docParent = $(doc1).parent().parent();

		if ("nestable3" == doc1.attr("id")) {
			return 0;
		} else {
			staticDepth++;
			chkDepthLoop(docParent);
		}

	}

	//메뉴 수정
	function editMenu(menuId) {
		$("#menu_nm").val($(".menuList" + menuId).find(".menu_nm").html());
		$("#menu_id").val($(".menuList" + menuId).attr("data-id"));
		$("#menu_type").val($(".menuList" + menuId).attr("menu_type"));
		if ("Y" == $(".menuList" + menuId).attr("user_write")) {
			$("#user_write").prop("checked", true);
		} else {
			$("#user_write").prop("checked", false);
		}

		if ("Y" == $(".menuList" + menuId).attr("use_yn")) {
			$("#use_yn").prop("checked", true);
		} else {
			$("#use_yn").prop("checked", false);
		}
		showBtn();
	}

	//메뉴 삭제
	function deleteMenu(menuId) {
		if (confirm("삭제하시겠습니까?")) {
			$(".menuList" + menuId).remove();
		}
	}

	var chkBtn = false;
	//디비에 저장
	function dbSave() {
		if (chkBtn) {
			alert("잠시만 기다리세요 진행중입니다.");
			return false;
		}

		if (confirm("저장하시겠습니까?")) {
			$("#btnDbSave").html("진행중...");
			chkBtn = true;
			$("#menuOl").find("button").remove();
			$("#menuList").val($("#menuOl").html());
			chkDepth();
			var menu_seqs = "<input name='menu_seqs' type='text' value='";
			var menu_nms = "<input name='menu_nms' type='text' value='";
			var menu_depths = "<input name='menu_depths' type='text' value='";
			var menu_urls = "<input name='menu_urls' type='text' value='";
			var menu_types = "<input name='menu_types' type='text' value='";
			var view_stats = "<input name='view_stats' type='text' value='";
			var user_writes = "<input name='user_writes' type='text' value='";
			var use_yns = "<input name='use_yns' type='text' value='";

			$(".dd-item").each(
					function(i) {
						if (i != 0) {
							menu_seqs += ";";
							menu_nms += ";";
							menu_depths += ";";
							menu_urls += ";";
							menu_types += ";";
							view_stats += ";";
							user_writes += ";";
							use_yns += ";";
						}
						menu_seqs += $(this).attr("data-id") == "" ? " " : $(
								this).attr("data-id");
						menu_nms += $(this).attr("menu_nm") == "" ? " " : $(
								this).attr("menu_nm");
						menu_depths += $(this).attr("menu_depth") == "" ? " "
								: $(this).attr("menu_depth");
						menu_urls += $(this).attr("menu_url") == "" ? " " : $(
								this).attr("menu_url");
						menu_types += $(this).attr("menu_type") == "" ? " "
								: $(this).attr("menu_type");
						view_stats += $(this).attr("view_stat") == "" ? " "
								: $(this).attr("view_stat");
						user_writes += $(this).attr("user_write") == "" ? " "
								: $(this).attr("user_write");
						use_yns += $(this).attr("use_yn") == "" ? " " : $(this)
								.attr("use_yn");
					});
			menu_seqs += "'>";
			menu_nms += "'>";
			menu_depths += "'>";
			menu_urls += "'>";
			menu_types += "'>";
			view_stats += "'>";
			user_writes += "'>";
			use_yns += "'>";

			$("#frmSave").append(menu_seqs);
			$("#frmSave").append(menu_nms);
			$("#frmSave").append(menu_depths);
			$("#frmSave").append(menu_urls);
			$("#frmSave").append(menu_types);
			$("#frmSave").append(view_stats);
			$("#frmSave").append(user_writes);
			$("#frmSave").append(use_yns);

			$("#frmSave").submit();
		}

	}

	$(document).ready(function() {
//		alert(1);
		$('#nestable3').nestable({placeClass: "ui-state-highlight"});
	});
